var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity_id: 0,
    realname: '',
    tel: '',
    is_default: 0,
    detail: {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options) {
      // var id = options.id;
      // if (options.scene) {
      //   id = decodeURIComponent(options.scene);
      // }
      if (options.id) {
        this.setData({
          activity_id: options.id
        })
      }

    }
    this.getInputData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.payment_id > 0) {
      this.getCardInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },

  //户名输入
  getRealname: function(e) {
    this.setData({
      realname: e.detail.value
    });
  },

  //手机号输入
  getTel: function(e) {
    this.setData({
      tel: e.detail.value
    });
  },

  getDefault: function(e) {
    console.log(e);
    this.setData({
      is_default: e.detail.value
    });
  },

  submitAdd: function() {
    var page = this;
    if (!page.data.realname || !page.data.tel) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return false;
    } else {
      page.addCard();
    }
  },
  getInputData:function(){
    var page = this;
    app.request({
      url: api.activity.get_act_input,
      method: "post",
      data: {
        activity_id: page.data.activity_id,
      },
      success: function (result) {
        wx.hideLoading();
        if (result.errcode == 0) {
          page.setData({
            input_data:result.data
          })
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: result.errmsg,
            showCancel: false
          });
        }
      }
    });
  },

  addCard: function() {
    wx.showLoading({
      title: '提交中',
    })
    var page = this;
    app.request({
      url: api.activity.join,
      method: 'POST',
      data: {
        id: page.data.activity_id,
        realname: page.data.realname,
        tel: page.data.tel,
        detail: JSON.stringify(page.data.detail),
      },
      success: function(res) {
        if (res.errcode == 0) {
          if (res.data.need_pay == 1) {
            //需要支付
            wx.showLoading({
              title: '准备支付',
            })
            app.request({
              url: api.order.get_pay_data,
              method: "post",
              data: {
                order_id: res.data.order_id,
                pay_type: 'WECHAT_PAY'
              },
              success: function(result) {
                wx.hideLoading();
                if (result.errcode == 0) {
                  var pay_data = result.data;
                  wx.requestPayment({
                    timeStamp: pay_data.timeStamp,
                    nonceStr: pay_data.nonceStr,
                    package: pay_data.package,
                    signType: pay_data.signType,
                    paySign: pay_data.paySign,
                    success: function(res) {
                      wx.showToast({
                        title: '报名成功',
                        icon: 'success'
                      });
                      setTimeout(function() {
                        wx.navigateBack({
                          delta: 1
                        })
                      }, 1500)
                    },
                    fail: function(res) {
                      wx.showToast({
                        title: '订单未支付',
                        image: '/images/icon-warning.png'
                      });
                    }
                  });
                } else {
                  wx.hideLoading();
                  wx.showModal({
                    title: '提示',
                    content: result.errmsg,
                    showCancel: false
                  });
                }
              }
            });
          } else {
            //不需支付
            wx.showToast({
              title: res.data.msg,
            });
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }

        }else{
          wx.showModal({
            title: '提示',
            content: res.errmsg,
            showCancel: false
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  //支付
  payOrder: function(order_id) {
    var page = this;

    app.request({
      url: api.order.get_pay_data,
      method: "post",
      data: {
        order_id: order_id,
        pay_type: 'WECHAT_PAY'
      },
      success: function(result) {
        wx.hideLoading();
        if (result.errcode == 0) {
          var pay_data = result.data;
          wx.requestPayment({
            timeStamp: pay_data.timeStamp,
            nonceStr: pay_data.nonceStr,
            package: pay_data.package,
            signType: pay_data.signType,
            paySign: pay_data.paySign,
            success: function(res) {
              wx.showToast({
                title: '报名成功',
                icon: 'success'
              });
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
            },
            fail: function(res) {
              wx.showToast({
                title: '订单未支付',
                image: '/images/icon-warning.png'
              });
            }
          });
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: result.errmsg,
            showCancel: false
          });
        }
      }
    });
  },
  clickUpimg: function (e) {
    console.log(e);
    
    var that = this;
    that.setData({
      tmp_name: e.currentTarget.dataset.name
    })
    app.upload(that.setPhoto);
  },
  setPhoto: function (re) {
    console.log(re);
    var that = this
    var detail = that.data.detail
    var name = that.data.tmp_name
    detail[name] = re.url
    that.setData({
      detail: detail
    })

  },
  inputOther:function(e){
    var name = e.currentTarget.dataset.name
    var detail = this.data.detail
    detail[name] = e.detail.value
    this.setData({
      detail:detail
    })
  },

  clickInchPhoto: function() {
    var that = this;
    app.upload(that.setInchPhoto);
  },
  setInchPhoto: function(re) {
    console.log(re);
    var that = this
    var detail = that.data.detail

    detail['inch_photo'] = re.url
    that.setData({
      detail: detail
    })

  },

})